package com.baozheyangken.ai_learn.service.serviceImpl;

import com.baozheyangken.ai_learn.mapper.DifficultyLevelMapper;
import com.baozheyangken.ai_learn.mapper.QuestionMapper;
import com.baozheyangken.ai_learn.mapper.UserMapper; // 假设存在 UserMapper 用于查询用户信息
import com.baozheyangken.ai_learn.pojo.entity.Question;
import com.baozheyangken.ai_learn.pojo.entity.QuestionType;
import com.baozheyangken.ai_learn.service.QuestionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.server.ResponseStatusException;
import static org.springframework.http.HttpStatus.BAD_REQUEST;

import java.util.List;
import java.util.Map;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private DifficultyLevelMapper difficultyLevelMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    @Transactional // 保证事务一致性
    public int addQuestion(Question question, List<Integer> knowledgeIds) {
        // 验证difficulty_id是否存在
        Integer difficultyId = question.getDifficultyId();
        // 修改方法调用名
        if (difficultyLevelMapper.getDifficultyLevelById(difficultyId.longValue()) == null) {
            throw new IllegalArgumentException("传入的difficulty_id不存在");
        }

        // 验证 user_id 是否存在
        if (!userMapper.existsById(question.getUserId())) {
            throw new ResponseStatusException(BAD_REQUEST, "用户 ID 不存在");
        }

        // 新增题目
        int result = questionMapper.addQuestion(question);
        if (result > 0 && knowledgeIds != null) {
            for (Integer knowledgeId : knowledgeIds) {
                questionMapper.addQuestionKnowledge(question.getId(), knowledgeId);
            }
        }
        return result;
    }

    @Override
    @Transactional
    public int deleteQuestionById(Integer id) {
        // 1. 删除知识点关联
        questionMapper.deleteQuestionKnowledge(id);
        // 2. 删除题目
        return questionMapper.deleteQuestionById(id);
    }

    @Override
    @Transactional
    public int updateQuestion(Question question, List<Integer> knowledgeIds) {
        // 1. 更新题目信息
        int result = questionMapper.updateQuestion(question);
        if (result > 0) {
            // 2. 先删除旧关联，再添加新关联
            questionMapper.deleteQuestionKnowledge(question.getId());
            if (knowledgeIds != null) {
                for (Integer knowledgeId : knowledgeIds) {
                    questionMapper.addQuestionKnowledge(question.getId(), knowledgeId);
                }
            }
        }
        return result;
    }

    @Override
    public List<Question> getAllQuestions(Map<String, Object> params) {
        //todo 添加关联知识点 setKnowledgePoints()
        return questionMapper.getAllQuestions(params);
    }

    @Override
    public List<QuestionType> getQuestionType() {
        return questionMapper.getQuestionType();
    }
}